/**
 * 
 */
package com.uit.trip_.data;

import java.util.ArrayList;
import java.util.List;

import com.uit.trip_.models.WallAppItem;
import com.uit.trip_.models.WallMenuItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author duongnx
 * 
 */
public class TripDatabase {

	private SQLiteDatabase db;
	private TripDatabaseHelper dbHelper;
	private String LOG = "db";

	public TripDatabase(Context context) {
		dbHelper = new TripDatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public long insertToTrip(WallMenuItem item) {
		Log.i(LOG, "item:" + item.itemName);
		ContentValues values = new ContentValues();
		values.put(TripDatabaseHelper.TRIP_NAME, item.itemName);
		values.put(TripDatabaseHelper.TRIP_DESCRIPTION, item.itemDesription);
		values.put(TripDatabaseHelper.TRIP_DATE, item.itemDate);

		return db.insert(TripDatabaseHelper.TABLE_TRIP, null, values);
	}

	public long insertToNote(WallAppItem item) {
		ContentValues values = new ContentValues();
		values.put(TripDatabaseHelper.NOTE_TRIP_ID, item.id_trip);
		values.put(TripDatabaseHelper.NOTE_CONTENT, item.content);
		values.put(TripDatabaseHelper.NOTE_LOCATION, item.address);
		values.put(TripDatabaseHelper.NOTE_DATE, item.date);

		return db.insert(TripDatabaseHelper.TABLE_NOTE, null, values);
	}

	public void insertToImage(int note_id, String path) {
		ContentValues values = new ContentValues();
		values.put(TripDatabaseHelper.IMAGE_NOTE_ID, note_id);
		values.put(TripDatabaseHelper.IMAGE_PATH, path);

		db.insert(TripDatabaseHelper.TABLE_IMAGE, null, values);
	}

	public List<WallMenuItem> getListTrips() {
		List<WallMenuItem> list = new ArrayList<WallMenuItem>();
		String sql = "select * from " + TripDatabaseHelper.TABLE_TRIP;
		Cursor c = db.rawQuery(sql, null);

		if (c.moveToFirst()) {
			do {
				int id = c.getInt(0);
				String description = c.getString(1);
				String name = c.getString(2);
				String date = c.getString(3);
				WallMenuItem item = new WallMenuItem(id, name, description,
						date);
				list.add(item);

			} while (c.moveToNext());
		}
		c.close();
		return list;
	}

	public List<WallAppItem> getListNoteById(int id) {
		List<WallAppItem> list = new ArrayList<WallAppItem>();
		String sql = "select * from " + TripDatabaseHelper.TABLE_NOTE
				+ " where " + TripDatabaseHelper.NOTE_TRIP_ID + " =" + id;
		Cursor c = db.rawQuery(sql, null);

		if (c.moveToFirst()) {
			do {
				int note_id = c.getInt(0);
				int trip_id = c.getInt(1);
				String content = c.getString(2);
				String address = c.getString(3);
				String date = c.getString(4);
				String imgPath = c.getString(5);

				WallAppItem item = new WallAppItem(note_id, trip_id, content,
						date, address, imgPath);
				list.add(item);
			} while (c.moveToNext());
		}
		c.close();
		return list;

	}

	/*
	 * Collection of Update Functions
	 */
	public void updateToNote(WallAppItem item) {
		ContentValues values = new ContentValues();
		values.put(TripDatabaseHelper.NOTE_TRIP_ID, item.id_trip);
		values.put(TripDatabaseHelper.NOTE_CONTENT, item.content);
		values.put(TripDatabaseHelper.NOTE_LOCATION, item.address);
		values.put(TripDatabaseHelper.NOTE_IMAGE_PATH, item.imagePath);

		String[] args = new String[] { String.valueOf(item.id) };

		db.update(TripDatabaseHelper.TABLE_NOTE, values, "_id=?", args);
	};

	/*
	 * Collection of Delete funtions
	 */
	public void deleteNoteWithId(int noteId) {
		db.delete(TripDatabaseHelper.TABLE_NOTE, " _id=?",
				new String[] { String.valueOf(noteId) });
	}

	public void deleteNoteWithTripId(int tripId) {
		db.delete(TripDatabaseHelper.TABLE_NOTE, " trip_id=?",
				new String[] { String.valueOf(tripId) });
	}

	public void deleteTripWithId(int tripId) {
		db.delete(TripDatabaseHelper.TABLE_TRIP, " _id=?",
				new String[] { String.valueOf(tripId) });
	}
}
